
  updateImages();
  function updateImages(){
            $.post("'.base_url("/images/ajax_list").'", { parent_id: $("#id").val(),table_id:3 },
            function(data) {
              $("#side_bar_block_image").html(data).hide("slow").delay(200).show("slow");
              $(".image_list").mouseover(function() {
                $(this).find("a").css("display","block");
              }).mouseout(function(){
                $(this).find("a").css("display","none");
              });
              $("#side_bar_block_image img").click(function() {
                $(this).addImg();
              });

              $("#side_bar_block_image a").click(function() {
                //$(this).delImg();
                //console.log($(this).prop("href"));
                $.post($(this).prop("href"), {},
                  function(data) {
                    //console.log(data);
                    var data = jQuery.parseJSON(data);
                    //console.log(data);
                    if(data.success == "1"){
                      updateImages();
                    }
                  }
                );
                return false;

              });

            });
  }

  (function($){
      $.fn.addImg = function(){
          tinyMCE.execCommand('mceInsertContent',false,'<img src="'+this.attr('src')+'"/>');
      };
  })(jQuery);

  //Uploader Control
  $("#btnShow").css("display", "block");
  $("#btnHide").css("display", "none");
  $("#uploader").hide();
  $("#btnHide").click(function() {
    $("#uploader").hide("slow");
    $("#btnShow").css("display", "block");
    $("#btnHide").css("display", "none");
  });

  $("#btnShow").click(function() {
    $("#uploader").show("slow");
    $("#btnShow").css("display", "none");
    $("#btnHide").css("display", "block");
    $("#btnHide").addClass("hide-button");
  });


  $("#uploader").pluploadQueue({
    // General settings
    runtimes : 'html5',
    url : ''.base_url("/images/ajax_upload").'',
    max_file_size : '10mb',
    chunk_size : '1mb',
    unique_names : true,


    // Resize images on clientside if we can
    //resize : {width : 320, height : 240, quality : 90},

    // Specify what files to browse for
    filters : [
      {title : "Image files", extensions : "jpg,gif,png"},
      {title : "Zip files", extensions : "zip"}
    ],

    // Flash settings
    flash_swf_url : '/plupload/js/plupload.flash.swf',

    // Silverlight settings
    silverlight_xap_url : '/plupload/js/plupload.silverlight.xap',

    init : {
      FilesAdded: function(up, files) {
        //autoSave();
        var $uploader = $("#uploader").pluploadQueue();
        $uploader.settings.multipart_params = {parent_id: $("#id").val(), table_id:2};
      },
      StateChanged: function(up) {
        // Called when the state of the queue is changed
        //log('[StateChanged]', up.state == plupload.STARTED ? "STARTED" : "STOPPED");
        var uploader = $('#uploader').pluploadQueue();

        if(up.state == plupload.STOPPED){
          setTimeout(function() {
                $( "#uploader" ).hide('slow').delay(500).show('slow');
                uploader.splice();
                $(".plupload_buttons").css("display", "inline");
                $(".plupload_upload_status").css("display", "inline");
                $(".plupload_start").addClass("plupload_disabled");
                $(".plupload_upload_status").css("display", "none");
                uploader.refresh();
          }, 100 );

        }
      },

      FileUploaded: function(up, file, info) {
        // Called when a file has finished uploading
        //log('[FileUploaded] File:', file, "Info:", info);
        console.log(info);
        plupload.each(info, function(value, key) {

          if(key == "response"){
            var value = jQuery.parseJSON(value);
            if(value.result == "TRUE"){
              updateImages();
            }
          }
        });


        //$( "#side_bar_block_image" ).delay(100).fadeIn(1000);

      }
    }
  });
